package com.info.zhiduoduo.admin.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageRowBounds;
import com.info.zhiduoduo.admin.model.formbean.commonproblem.CommonProblemFormBean;
import com.info.zhiduoduo.admin.model.formbean.commonproblem.GetCommonProblemFormBean;
import com.info.zhiduoduo.admin.model.formbean.commonproblem.UpdateCommonProblemFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.repository.entity.commonproblem.CommonProblemEntity;
import com.info.zhiduoduo.repository.mapper.commonproblem.CommonProblemMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * 该service中的增删改查全都是通过mybatis-plus的内部方法完成
 * @author Trink丶
 */

@Slf4j
@Service
public class CommonProblemService {

    @Autowired
    private CommonProblemMapper commonProblemMapper;

    /**
     * 增加常见问题
     *
     * @param formBean reqdata中传入需要增加的值
     * @return 返回值
     */
    public SmartResult saveCommonProblem(BaseSimpleFormBean<CommonProblemFormBean> formBean) {
        CommonProblemEntity insert = new CommonProblemEntity();
        BeanUtils.copyProperties(formBean.getReqdata(), insert);
        commonProblemMapper.insert(insert);
        return SmartResult.success();
    }

    /**
     * 修改常见问题
     *
     * @param formBean reqdata中传入需要修改的值
     * @return 返回值
     */
    public SmartResult updateCommonProblem(BaseSimpleFormBean<UpdateCommonProblemFormBean> formBean) {
        CommonProblemEntity update = new CommonProblemEntity();
        BeanUtils.copyProperties(formBean.getReqdata(), update);
        commonProblemMapper.updateById(update);
        return SmartResult.success();
    }


    /**
     * 删除常见问题
     *
     * @param formBean reqdata中传入需要删除的id
     * @return 返回值
     */
    public SmartResult deleteCommonProblem(BaseSimpleFormBean<Integer> formBean) {
        commonProblemMapper.deleteById(formBean.getReqdata());
        return SmartResult.success();
    }

    /**
     * 批量删除常见问题
     *
     * @param formBean reqdata中传入需要删除的id
     * @return 返回值
     */
    public SmartResult deleteCommonProblemList(BaseSimpleFormBean<List<Integer>> formBean) {
        List<Integer> delete = formBean.getReqdata();
        commonProblemMapper.deleteBatchIds(delete);
        return SmartResult.success();
    }

    /**
     * 查询常见问题
     *
     * @param formBean reqdata中传入需要查询的类型
     * @return 返回值
     */
    public SmartResult getCommonProblemList(BaseSimpleFormBean<GetCommonProblemFormBean> formBean) {
        /*
          配置分页信息
         */
        int page = formBean.getPage();
        int rows = formBean.getRows();
        if (page <= 0) {
            return SmartResult.fail().setMsg("分页信息出现问题，请联系管理员");
        }
        int start = page * rows - rows;
        /*
          查询条件
         */
        GetCommonProblemFormBean condition = formBean.getReqdata();
        Integer type = condition.getType();
        String msg = condition.getMsg();
        EntityWrapper<CommonProblemEntity> wrapper = new EntityWrapper<CommonProblemEntity>();
        wrapper
                .eq(null != type, "problem_type", type)
                .like(null != msg && msg.trim().length() != 0, "problem_description", msg,SqlLike.DEFAULT)
                .or(null != msg && msg.trim().length() != 0,"")
                .like(null != msg && msg.trim().length() != 0, "problem_respond", msg, SqlLike.DEFAULT);
        /*
          查询
         */
        PageRowBounds pageRowBounds = new PageRowBounds(start,rows);
        List<CommonProblemEntity> result = commonProblemMapper.selectPage(pageRowBounds, wrapper);
        log.info(JSON.toJSONString(pageRowBounds));
        return SmartResult.success().setList(result).setTotalrecords(pageRowBounds.getTotal());
    }
}
